package com.greentravel.view;

import com.greentravel.model.User;
import com.greentravel.service.UserService;
import com.greentravel.service.impl.UserServiceImpl;
import com.greentravel.util.GUIUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 主界面
 * 系统的主要功能界面，包含各个功能模块的入口
 */
public class MainView extends JFrame {

    private User currentUser;
    private UserService userService;

    private JPanel contentPanel;
    private JButton userProfileButton;
    private JButton routeButton;
    private JButton sharedVehicleButton;
    private JButton newsButton;
    private JButton travelRecordButton;
    private JButton pointsButton;
    private JButton carbonButton;
    private JButton rankButton;
    private JButton dataAnalysisButton;
    private JButton momentButton;
    private JButton logoutButton;

    /**
     * 构造方法，初始化主界面
     * 
     * @param user 当前登录用户
     */
    public MainView(User user) {
        this.currentUser = user;
        this.userService = new UserServiceImpl();
        initComponents();
    }

    /**
     * 初始化界面组件
     */
    private void initComponents() {
        // 设置窗口基本属性
        setTitle("绿色出行倡导系统 - 主界面");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 设置UI样式
        GUIUtil.setUIStyle();

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建顶部面板
        JPanel topPanel = createTopPanel();

        // 创建侧边导航面板
        JPanel sidePanel = createSidePanel();

        // 创建内容面板
        contentPanel = new JPanel();
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        contentPanel.setLayout(new BorderLayout());

        // 默认显示欢迎内容
        showWelcome();

        // 将面板添加到主面板
        mainPanel.add(topPanel, BorderLayout.NORTH);
        mainPanel.add(sidePanel, BorderLayout.WEST);
        mainPanel.add(contentPanel, BorderLayout.CENTER);

        // 添加主面板到窗口
        setContentPane(mainPanel);
    }

    /**
     * 创建顶部面板
     * 
     * @return 顶部面板
     */
    private JPanel createTopPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(GUIUtil.PRIMARY_COLOR);
        panel.setBorder(new EmptyBorder(10, 15, 10, 15));

        // 标题标签
        JLabel titleLabel = new JLabel("绿色出行倡导与数据记录系统");
        titleLabel.setFont(GUIUtil.TITLE_FONT);
        titleLabel.setForeground(Color.WHITE);

        // 用户信息标签
        JLabel userLabel = new JLabel("欢迎, " + currentUser.getName());
        userLabel.setFont(GUIUtil.NORMAL_FONT);
        userLabel.setForeground(Color.WHITE);

        panel.add(titleLabel, BorderLayout.WEST);
        panel.add(userLabel, BorderLayout.EAST);

        return panel;
    }

    /**
     * 创建侧边导航面板
     * 
     * @return 侧边导航面板
     */
    private JPanel createSidePanel() {
        JPanel panel = new JPanel(new GridLayout(12, 1, 0, 5));
        panel.setBackground(GUIUtil.SECONDARY_COLOR);
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));
        panel.setPreferredSize(new Dimension(200, getHeight()));

        // 创建导航按钮
        userProfileButton = createNavButton("个人信息");
        routeButton = createNavButton("线路规划");
        sharedVehicleButton = createNavButton("共享交通工具");
        newsButton = createNavButton("环保资讯");
        travelRecordButton = createNavButton("出行记录");
        pointsButton = createNavButton("积分查询");
        carbonButton = createNavButton("碳减排计算");
        rankButton = createNavButton("排行榜");
        dataAnalysisButton = createNavButton("数据分析");
        momentButton = createNavButton("好友动态");
        logoutButton = createNavButton("退出登录");

        // 添加按钮到面板
        panel.add(userProfileButton);
        panel.add(routeButton);
        panel.add(sharedVehicleButton);
        panel.add(newsButton);
        panel.add(travelRecordButton);
        panel.add(pointsButton);
        panel.add(carbonButton);
        panel.add(rankButton);
        panel.add(dataAnalysisButton);
        panel.add(momentButton);
        panel.add(new JLabel()); // 空白填充
        panel.add(logoutButton);

        // 添加按钮事件
        addButtonListeners();

        return panel;
    }

    /**
     * 创建导航按钮
     * 
     * @param text 按钮文本
     * @return 按钮
     */
    private JButton createNavButton(String text) {
        JButton button = new JButton(text);
        button.setFont(GUIUtil.NORMAL_FONT);
        button.setBackground(GUIUtil.SECONDARY_COLOR);
        button.setForeground(Color.BLACK);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setHorizontalAlignment(SwingConstants.LEFT);

        // 鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(GUIUtil.PRIMARY_COLOR);
                button.setForeground(Color.WHITE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(GUIUtil.SECONDARY_COLOR);
                button.setForeground(Color.BLACK);
            }
        });

        return button;
    }

    /**
     * 添加按钮事件监听器
     */
    private void addButtonListeners() {
        userProfileButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showUserProfile();
            }
        });

        routeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showRouteView();
            }
        });

        sharedVehicleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showSharedVehicleView();
            }
        });

        newsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showNewsView();
            }
        });

        travelRecordButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showTravelRecordView();
            }
        });

        pointsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPointsView();
            }
        });

        carbonButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showCarbonView();
            }
        });

        rankButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showRankView();
            }
        });

        dataAnalysisButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showDataAnalysisView();
            }
        });

        momentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showMomentView();
            }
        });

        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                logout();
            }
        });
    }

    /**
     * 显示欢迎内容
     */
    private void showWelcome() {
        contentPanel.removeAll();

        JPanel welcomePanel = new JPanel(new BorderLayout());
        welcomePanel.setBackground(Color.WHITE);

        JLabel welcomeLabel = new JLabel("欢迎使用绿色出行倡导与数据记录系统", SwingConstants.CENTER);
        welcomeLabel.setFont(GUIUtil.TITLE_FONT);
        welcomeLabel.setForeground(GUIUtil.PRIMARY_COLOR);

        JLabel descLabel = new JLabel("<html><div style='text-align: center;'>请从左侧菜单选择功能<br>一起为环保出一份力!</div></html>",
                SwingConstants.CENTER);
        descLabel.setFont(GUIUtil.NORMAL_FONT);

        welcomePanel.add(welcomeLabel, BorderLayout.CENTER);
        welcomePanel.add(descLabel, BorderLayout.SOUTH);

        contentPanel.add(welcomePanel, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示用户个人信息界面
     */
    private void showUserProfile() {
        contentPanel.removeAll();

        // 获取最新的用户信息
        currentUser = userService.getUserInfo(currentUser.getId());

        // 创建个人信息面板
        UserProfileView profileView = new UserProfileView(currentUser);

        contentPanel.add(profileView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示线路规划界面
     */
    private void showRouteView() {
        contentPanel.removeAll();

        // 创建线路规划面板
        RouteView routeView = new RouteView(currentUser);

        contentPanel.add(routeView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示共享交通工具界面
     */
    private void showSharedVehicleView() {
        contentPanel.removeAll();

        // 创建共享交通工具面板
        SharedVehicleView sharedVehicleView = new SharedVehicleView(currentUser);

        contentPanel.add(sharedVehicleView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示环保资讯界面
     */
    private void showNewsView() {
        contentPanel.removeAll();

        // 创建环保资讯面板
        NewsView newsView = new NewsView();

        contentPanel.add(newsView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示出行记录界面
     */
    private void showTravelRecordView() {
        contentPanel.removeAll();

        // 创建出行记录面板
        TravelRecordView travelRecordView = new TravelRecordView(currentUser);

        contentPanel.add(travelRecordView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示积分查询界面
     */
    private void showPointsView() {
        contentPanel.removeAll();

        // 创建积分查询面板
        PointsView pointsView = new PointsView(currentUser);

        contentPanel.add(pointsView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示碳减排计算界面
     */
    private void showCarbonView() {
        contentPanel.removeAll();

        // 创建碳减排计算面板
        CarbonView carbonView = new CarbonView(currentUser);

        contentPanel.add(carbonView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示排行榜界面
     */
    private void showRankView() {
        contentPanel.removeAll();

        // 创建排行榜面板
        RankView rankView = new RankView();

        contentPanel.add(rankView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示数据分析界面
     */
    private void showDataAnalysisView() {
        contentPanel.removeAll();

        // 创建数据分析面板
        DataAnalysisView dataAnalysisView = new DataAnalysisView(currentUser);

        contentPanel.add(dataAnalysisView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 显示好友动态界面
     */
    private void showMomentView() {
        contentPanel.removeAll();

        // 创建好友动态面板
        MomentView momentView = new MomentView(currentUser);

        contentPanel.add(momentView, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();
    }

    /**
     * 退出登录
     */
    private void logout() {
        if (GUIUtil.showConfirmDialog(this, "确定要退出登录吗？")) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    LoginView loginView = new LoginView();
                    loginView.setVisible(true);
                    dispose(); // 关闭主界面
                }
            });
        }
    }
}